﻿using furion.blog.Domain.Shared;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmailSendLogModel = furion.blog.Domain.Mongo.EmailSendLog;

namespace furion.blog.Domain.Query.EmailSendLog
{
    public class EmailSendLogListQuery : MongoBaseQuery
    {
        public string SearchContent { get; set; } = string.Empty;
        public string Date { get; set; } = string.Empty;
        public string Status { get; set; } = string.Empty;

        public FilterDefinition<T> Filter<T>() where T : EmailSendLogModel
        {
            FilterDefinitionBuilder<T> builder = Builders<T>.Filter;
            List<FilterDefinition<T>> queryParams = new List<FilterDefinition<T>>();
            if (!string.IsNullOrWhiteSpace(Date))
            {
                queryParams.Add(builder.Gte(t => t.CreatedTime, DateTime.Parse(Date + " 00:00:00")));
                queryParams.Add(builder.Lte(t => t.CreatedTime, DateTime.Parse(Date + " 23:59:59")));
            }
            if (!string.IsNullOrWhiteSpace(Status)) {
                queryParams.Add(builder.Eq(t => t.Result,Status));
            }
            if (!string.IsNullOrWhiteSpace(SearchContent))
            {
                queryParams.Add(builder.Where(t => (t.From.Contains(SearchContent) || t.To.Contains(SearchContent) || t.Cc.Contains(SearchContent)|| t.Subject.Contains(SearchContent) || t.Content.Contains(SearchContent))));
            }
            return builder.And(queryParams);
        }


    }
}
